package com.cong._37;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.*;

public class SolutionTest_37 {

    @Test
    public void solveSudoku() {
        char[][] boards = new char[][] {
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}
        };
        char[][] originBoard = new char[][] {
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}
        };
        char[][] res = new char[][] {
                {'5','3','1','2','7','4','6','9','8'},
                {'6','2','3','1','9','5','8','4','7'},
                {'1','9','8','3','4','2','7','6','5'},
                {'8','1','2','5','6','7','9','4','3'},
                {'4','5','7','8','6','3','4','2','1'},
                {'7','4','5','9','2','8','1','3','6'},
                {'9','6','3','4','5','1','2','8','6'},
                {'2','7','4','4','1','9','3','5','5'},
                {'3','8','6','7','8','4','5','7','9'}
        };
        new Solution().solveSudoku(boards);
        for(char[] cs : boards) {
            System.out.println(Arrays.toString(cs));
        }
        // 按行检查
        for(int i = 0;i < 9;i++) {
            boolean repeat = false;
            Set<Character> s = new HashSet<>();
            for (int j = 0;j < 9;j++) {
                if (!s.add(boards[i][j])) {
                    System.out.println("failed at line: " + i);
                }
            }
//            System.out.println(s.size());
        }

        for(int j = 0;j < 9;j++) {
            boolean repeat = false;
            Set<Character> s = new HashSet<>();
            for (int i = 0;i < 9;i++) {
                if (!s.add(boards[i][j])) {
                    System.out.println("failed at col: " + j);
                }
            }
//            System.out.println(s.size());
        }
        for(int i = 0;i < 9;i++) {
            for(int j = 0;j < 9;j++) {
                if (originBoard[i][j] != '.') {
                    if (originBoard[i][j] != boards[i][j]) {
                        System.out.println("failed at: " + i + ", " + j);
                    }
                }
            }
        }
    }

}